package com.prawn.trace.produce.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.prawn.trace.produce.dao.ProduceDao;
import com.prawn.trace.produce.pojo.Produce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import util.IdWorker;

/**
 * 服务层
 * 
 * @author Administrator
 *
 */
@Service
public class ProduceService {

	@Autowired
	private ProduceDao produceDao;
	
	@Autowired
	private IdWorker idWorker;

	/**
	 * 查询全部列表
	 * @return
	 */
	public List<Produce> findAll() {
		return produceDao.findAll();
	}

	
	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<Produce> findSearch(Map whereMap, int page, int size) {
		Specification<Produce> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return produceDao.findAll(specification, pageRequest);
	}

	
	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<Produce> findSearch(Map whereMap) {
		Specification<Produce> specification = createSpecification(whereMap);
		return produceDao.findAll(specification);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	public Produce findById(String id) {
		return produceDao.findById(id).get();
	}

	/**
	 * 增加
	 * @param produce
	 */
	public void add(Produce produce) {
		produce.setId( idWorker.nextId()+"" );
		produceDao.save(produce);
	}

	/**
	 * 修改
	 * @param produce
	 */
	public void update(Produce produce) {
		produceDao.save(produce);
	}

	/**
	 * 删除
	 * @param id
	 */
	public void deleteById(String id) {
		produceDao.deleteById(id);
	}

	/**
	 * 动态条件构建
	 * @param searchMap
	 * @return
	 */
	private Specification<Produce> createSpecification(Map searchMap) {

		return new Specification<Produce>() {

			@Override
			public Predicate toPredicate(Root<Produce> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // 产品id
                if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
                	predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%"));
                }
                // 产品名称
                if (searchMap.get("name")!=null && !"".equals(searchMap.get("name"))) {
                	predicateList.add(cb.like(root.get("name").as(String.class), "%"+(String)searchMap.get("name")+"%"));
                }
                // 产品图片
                if (searchMap.get("pic")!=null && !"".equals(searchMap.get("pic"))) {
                	predicateList.add(cb.like(root.get("pic").as(String.class), "%"+(String)searchMap.get("pic")+"%"));
                }
                // 产品介绍
                if (searchMap.get("pro_describe")!=null && !"".equals(searchMap.get("pro_describe"))) {
                	predicateList.add(cb.like(root.get("pro_describe").as(String.class), "%"+(String)searchMap.get("pro_describe")+"%"));
                }
                // 产品所属分类id
                if (searchMap.get("category_id")!=null && !"".equals(searchMap.get("category_id"))) {
                	predicateList.add(cb.like(root.get("category_id").as(String.class), "%"+(String)searchMap.get("category_id")+"%"));
                }
                // 产品所属批次id
                if (searchMap.get("batch_id")!=null && !"".equals(searchMap.get("batch_id"))) {
                	predicateList.add(cb.like(root.get("batch_id").as(String.class), "%"+(String)searchMap.get("batch_id")+"%"));
                }
                // 产品来源厂家id
                if (searchMap.get("manufactor_id")!=null && !"".equals(searchMap.get("manufactor_id"))) {
                	predicateList.add(cb.like(root.get("manufactor_id").as(String.class), "%"+(String)searchMap.get("manufactor_id")+"%"));
                }
                // 运输物流公司id
                if (searchMap.get("logistics_company_id")!=null && !"".equals(searchMap.get("logistics_company_id"))) {
                	predicateList.add(cb.like(root.get("logistics_company_id").as(String.class), "%"+(String)searchMap.get("logistics_company_id")+"%"));
                }
                // 销售单位id
                if (searchMap.get("sales_unit_id")!=null && !"".equals(searchMap.get("sales_unit_id"))) {
                	predicateList.add(cb.like(root.get("sales_unit_id").as(String.class), "%"+(String)searchMap.get("sales_unit_id")+"%"));
                }
                // 备注信息
                if (searchMap.get("remarks")!=null && !"".equals(searchMap.get("remarks"))) {
                	predicateList.add(cb.like(root.get("remarks").as(String.class), "%"+(String)searchMap.get("remarks")+"%"));
                }
                // 采用标准
                if (searchMap.get("adopt_standard")!=null && !"".equals(searchMap.get("adopt_standard"))) {
                	predicateList.add(cb.like(root.get("adopt_standard").as(String.class), "%"+(String)searchMap.get("adopt_standard")+"%"));
                }
                // 质检部门报告
                if (searchMap.get("quality_inspection_report")!=null && !"".equals(searchMap.get("quality_inspection_report"))) {
                	predicateList.add(cb.like(root.get("quality_inspection_report").as(String.class), "%"+(String)searchMap.get("quality_inspection_report")+"%"));
                }
                // 质量管理体系认证书
                if (searchMap.get("certificate_of_quality")!=null && !"".equals(searchMap.get("certificate_of_quality"))) {
                	predicateList.add(cb.like(root.get("certificate_of_quality").as(String.class), "%"+(String)searchMap.get("certificate_of_quality")+"%"));
                }
                // 产品许可证
                if (searchMap.get("product_license")!=null && !"".equals(searchMap.get("product_license"))) {
                	predicateList.add(cb.like(root.get("product_license").as(String.class), "%"+(String)searchMap.get("product_license")+"%"));
                }
                // 产地信息
                if (searchMap.get("origin_information")!=null && !"".equals(searchMap.get("origin_information"))) {
                	predicateList.add(cb.like(root.get("origin_information").as(String.class), "%"+(String)searchMap.get("origin_information")+"%"));
                }
                // 
                if (searchMap.get("create_by")!=null && !"".equals(searchMap.get("create_by"))) {
                	predicateList.add(cb.like(root.get("create_by").as(String.class), "%"+(String)searchMap.get("create_by")+"%"));
                }
				
				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

			}
		};

	}

}
